from django.db import models
from django.contrib.auth.models import AbstractUser
from company.models import Company,Department
from datetime import datetime, timedelta
# Create your models here.

class User(AbstractUser):
    """
    用户类
    """

    groups = None
    user_permissions = None
    nickname = models.CharField(max_length=100, blank=True, null=True, verbose_name='用户昵称')
    mobile = models.CharField(max_length=12, blank=True, null=True, verbose_name='手机号')
    avatar = models.FileField(verbose_name='头像', upload_to='image/user', blank=True, null=True)
    is_newuser = models.IntegerField(default=0,null=False,verbose_name="是否新用户")
    is_auth = models.IntegerField(default=0,null=False,verbose_name="是否认证")
    id_card = models.CharField(max_length=18,default='',null=True,verbose_name="身份证号")
    id_card_front_url = models.CharField(max_length=255,default='',null=True,verbose_name="身份证正面照片")
    id_card_front_image_id = models.CharField(max_length=18,default='',null=True,verbose_name="身份证正面照片id")
    id_card_back_url = models.CharField(max_length=255,default='',null=True,verbose_name="身份证反面照片")
    id_card_back_image_id = models.CharField(max_length=18,default='',null=True,verbose_name="身份证反面照片id")
    company = models.ForeignKey(Company,max_length=18,default='',blank=True,null=True,verbose_name="公司id",on_delete=models.SET_NULL,related_name='company')
    department = models.ForeignKey(Department,max_length=18,default='',null=True,verbose_name="Departmentid",on_delete=models.SET_NULL,related_name='Department')
    company_name = models.CharField(max_length=255,default='',null=True,blank=True,verbose_name="冗余公司名称")
    type = models.IntegerField(default=0,null=False,verbose_name="用户类型：0-普通用户，1-企业管理员 2-企业员工，3企业负责人")
    creator_user = models.ForeignKey('self', verbose_name='创建者', blank=True, null=True,
                                     related_name='user_creator_user', on_delete=models.SET_NULL)
    group_id = models.CharField(max_length=100, blank=True, null=True, verbose_name='用户组id')
    back_reason = models.CharField(max_length=255, default='', blank=True, null=True, verbose_name="驳回用户申请原因")

    class Meta:
        verbose_name = '用户管理'
        verbose_name_plural = verbose_name
        app_label = 'user'  # 添加这一行，指定所属的应用程序标签

    def __str__(self):
        return self.username


    def update(self, instance, validated_data):

        user = User.objects.get(pk=instance.pk)

        user.first_name = validated_data['first_name']
        user.id_card = validated_data['id_card']
        user.id_card_back_image_id = validated_data['id_card_back_image_id']
        user.id_card_back_url = validated_data['id_card_back_url']
        user.id_card_front_image_id = validated_data['id_card_front_image_id']
        user.id_card_front_url = validated_data['id_card_front_url']
        user.mobile = validated_data['mobile']
        user.nickname = validated_data['nickname']

        User.objects.filter(pk=instance.pk).update(**user)
        return instance


class UserGroup(models.Model):
    """
    用户角色类
    """
    rules = models.TextField(blank=True, null=True, verbose_name='权限ids,菜单ids',default='')
    name = models.CharField(default='', max_length=100, blank=True, null=True, verbose_name='角色名称')
    class Meta:
        verbose_name = '用户角色'
        verbose_name_plural = verbose_name
        app_label = 'user'  # 添加这一行，指定所属的应用程序标签
        default_permissions = ()
        ordering = ['name']

    def __str__(self):
        return self.name



class UserAccountBind(models.Model):
    """账号绑定"""
    user_id = models.CharField(max_length=12, blank=True, null=True, verbose_name='用户id')
    type = models.CharField(max_length=1, blank=True, null=True,default=0, verbose_name='类型：0：个人用户，1企业用户')
    company_id = models.CharField(max_length=100, blank=True, null=True, verbose_name='company_id')
    company_name = models.CharField(max_length=100, blank=True, null=True, verbose_name='冗余公司名称')
    username = models.CharField(max_length=12, blank=True, null=True, verbose_name='冗余用户名')
    platefrom_id = models.CharField(max_length=12, blank=False, null=True, verbose_name='平台id')
    en_name = models.CharField(max_length=12, blank=False, null=True, verbose_name='冗余平台名称')
    plate_account = models.CharField(max_length=12, blank=True, null=True, verbose_name='用户在平台的账号')
    app_account_upload = models.CharField(verbose_name='账户对应平台的手机端登录文件',default='',max_length=255, blank=True, null=True)
    add_time = models.DateTimeField(verbose_name='添加时间', default=datetime.now, blank=True, null=True)

    class Meta:
        verbose_name = '账号绑定'
        verbose_name_plural = verbose_name


class Employee(models.Model):
    """
    部门
    """
    company = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name='公司')
    department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name='部门')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    # kpi = models.CharField(max_length=100, default='',null=False, verbose_name='kpi任务')


    class Meta:
        verbose_name = '部门员工'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name



class Message(models.Model):
    """消息"""
    company = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name='公司')
    department = models.ForeignKey(Department,blank=True,null=True, on_delete=models.CASCADE, verbose_name='部门')
    user = models.ForeignKey(User,blank=True,null=True, on_delete=models.CASCADE, verbose_name='用户',related_name='user')
    title = models.CharField(max_length=100, verbose_name='主题')
    content = models.TextField(verbose_name='内容')
    is_read = models.SmallIntegerField(verbose_name='是否已读 0未读，1已读', default=0)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True, blank=True, null=True)

    class Meta:
        verbose_name = '消息列表'
        verbose_name_plural = verbose_name